package com.lanou.fooddemo.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.PicBean;
import com.lanou.fooddemo.bean.SubCardsBean;
import com.lanou.fooddemo.bean.UserBean;
import com.lanou.fooddemo.util.CommonUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dllo on 2015/8/13.
 */
public class FoodDatabaseTool {
    private Context context;
    private SQLiteDatabase database;

    public FoodDatabaseTool(Context context) {
        this.context = context;
        FoodDatabaseHelper helper = new FoodDatabaseHelper(context);
        database = helper.getWritableDatabase();
    }

    /**
     * 插入通知数据
     * @param title
     * @param content
     * @return
     */
    public long insertNotice(String title, String content) {
        ContentValues values = new ContentValues();
        values.put(FoodDatabaseHelper.CLOUMN_NAME_TITLE, title);
        values.put(FoodDatabaseHelper.CLOUMN_NAME_CONTENT, content);
        return database.insert(FoodDatabaseHelper.DATABASE_TABLE_NAME_NOTICE, null, values);
    }
    public List<HashMap<String, String>> selectNoticeAll() {
        //查询数据库。返回游标
        Cursor cursor = database.query(FoodDatabaseHelper.DATABASE_TABLE_NAME_NOTICE,
                new String[]{FoodDatabaseHelper.CLOUMN_NAME_TITLE, FoodDatabaseHelper.CLOUMN_NAME_CONTENT},
                null,
                null,
                null,
                null,
                null);
        //创建HashMap的List，存放数据
        List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>();
        //遍历游标
        while (cursor.moveToNext()) {
            //取出title值
            String title = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_TITLE));
            //取出内容值
            String content = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_CONTENT));
            //创建HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            //放入相应值
            map.put(title, content);
            //加入到List
            mapList.add(map);
        }
        //关闭游标
        cursor.close();
        return mapList;
    }

    /**
     * 插入菜篮子
     * @param dataBean
     * @return
     */
    public long insertBasket(DataBean dataBean) {
        String userIconUrl = null;
        String userName = null;
        String userSign = null;
        String foodUrl = null;
        String place = null;
        String foodContent = null;
        List<SubCardsBean> subCardsBeans = dataBean.getSubCards();
        if (CommonUtil.isListNotNull(subCardsBeans)) {
            for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
                if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
                    userIconUrl = subCardsBeanTemp.getUser().getAvatar();
                    userName = subCardsBeanTemp.getTitle();
                    userSign = subCardsBeanTemp.getDesc();
                }
                if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType()) ||
                        SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())) {
                    if (null == subCardsBeanTemp.getPics()) {
                        foodUrl = subCardsBeanTemp.getPic().getUrl();
                    } else {
                        foodUrl = subCardsBeanTemp.getPics().get(0).getUrl();
                    }
                }
                if (SubCardsBean.TYPE_MAP_CARD.equals(subCardsBeanTemp.getType())) {
                    place = subCardsBeanTemp.getTitle();
                }
                if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
                    foodContent = subCardsBeanTemp.getContent();
                }
            }
            ContentValues values = new ContentValues();
            values.put(FoodDatabaseHelper.CLOUMN_NAME_USER_ICON_URL, userIconUrl);
            values.put(FoodDatabaseHelper.CLOUMN_NAME_USER_NAME, userName);
            values.put(FoodDatabaseHelper.CLOUMN_NAME_USER_SIGN, userSign);
            values.put(FoodDatabaseHelper.CLOUMN_NAME_FOOD_URL, foodUrl);
            values.put(FoodDatabaseHelper.CLOUMN_NAME_PLACE, place);
            values.put(FoodDatabaseHelper.CLOUMN_NAME_FOOD_CONTENT, foodContent);
            return database.insert(FoodDatabaseHelper.DATABASE_TABLE_NAME_BASKET, null, values);
        }
        return -1;
    }

    /**
     * 返回全部菜篮子数据
     * @return
     */
    public List<DataBean> selectAllBasket() {
        //查询数据库。返回游标
        Cursor cursor = database.query(FoodDatabaseHelper.DATABASE_TABLE_NAME_BASKET,
                new String[]{FoodDatabaseHelper.CLOUMN_NAME_USER_ICON_URL,
                        FoodDatabaseHelper.CLOUMN_NAME_USER_NAME,
                        FoodDatabaseHelper.CLOUMN_NAME_USER_SIGN,
                        FoodDatabaseHelper.CLOUMN_NAME_FOOD_URL,
                        FoodDatabaseHelper.CLOUMN_NAME_PLACE,
                        FoodDatabaseHelper.CLOUMN_NAME_FOOD_CONTENT},
                null,
                null,
                null,
                null,
                null);
        List<DataBean> dataBeans = new ArrayList<DataBean>();
        while (cursor.moveToNext()) {
            String userIconUrl = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_USER_ICON_URL));
            String userName = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_USER_NAME));
            String userSign = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_USER_SIGN));
            String foodUrl = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_FOOD_URL));
            String place = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_PLACE));
            String foodContent = cursor.getString(cursor.getColumnIndex(FoodDatabaseHelper.CLOUMN_NAME_FOOD_CONTENT));

            List<SubCardsBean> subCardsBeans = new ArrayList<SubCardsBean>();

            SubCardsBean subCardsBeanUser = new SubCardsBean();
            subCardsBeanUser.setType(SubCardsBean.TYPE_USER_CARD);
            UserBean userBean = new UserBean();
            userBean.setAvatar(userIconUrl);
            subCardsBeanUser.setUser(userBean);
            subCardsBeanUser.setTitle(userName);
            subCardsBeanUser.setDesc(userSign);

            SubCardsBean subCardsBeanImage = new SubCardsBean();
            subCardsBeanImage.setType(SubCardsBean.TYPE_IMAGE_CARD);
            PicBean picBean = new PicBean();
            picBean.setUrl(foodUrl);
            subCardsBeanImage.setPic(picBean);

            SubCardsBean subCardsBeanMap = new SubCardsBean();
            subCardsBeanMap.setType(SubCardsBean.TYPE_MAP_CARD);
            subCardsBeanMap.setTitle(place);

            SubCardsBean subCardsBeanContent = new SubCardsBean();
            subCardsBeanContent.setType(SubCardsBean.TYPE_CONTENT_CARD);
            subCardsBeanContent.setContent(foodContent);

            subCardsBeans.add(subCardsBeanUser);
            subCardsBeans.add(subCardsBeanImage);
            subCardsBeans.add(subCardsBeanMap);
            subCardsBeans.add(subCardsBeanContent);

            DataBean dataBean = new DataBean();
            dataBean.setType(DataBean.TYPE_CONTAINER_CARD);
            dataBean.setSubCards(subCardsBeans);

            dataBeans.add(dataBean);
        }
        Collections.reverse(dataBeans);
        return dataBeans;
    }

    /**
     * 清空菜篮子
     * @return
     */
    public int deleteAllBasket() {
        return database.delete(FoodDatabaseHelper.DATABASE_TABLE_NAME_BASKET, null, null);
    }

    /**
     * 关闭数据库
     */
    public void close() {
        if (null != database) {
            database.close();
        }
    }
}
